﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using TankWin.Classes;
using System.Media;
using System.IO;

namespace TankWin
{
    public partial class frMain : Form
    {
        public static EStatusGUI _statusGUI = EStatusGUI.MainMenuGUI;
        GUI[] _GUI;

        public static Color _Back_Color = Color.Black;//mau nen cua game
        public static bool _makeNewGame = true;//danh dau la can phai tao game moi
        public static int _WINDOWS_WIDTH = 640;
        public static int _WINDOWS_HEIGHT = 640;
        public static long _highScore = 0;
        SoundPlayer _turnOn;
        public frMain()
        {
            _turnOn = new SoundPlayer();
            _turnOn.SoundLocation = @"C:\Windows\Media\Windows Logon Sound.wav";
            try
            {
                _turnOn.Load();
            }
            catch (System.Exception ex)
            {
            	
            }
            InitializeComponent();
            ObjMap.LoadMap(1);
            Graphics _g = this.CreateGraphics();
            _GUI = new GUI[8];
            _GUI[0] = new GUIPlay(_g);
            _GUI[1] = new GUIOption();
            _GUI[2] = new GUIConstruction();
            _GUI[3] = new GUIHighScore();
            _GUI[4] = new GUIAbout();
            _GUI[5] = new GUIExit();
            _GUI[6] = new GUIMainMenu();
            _GUI[7] = new GUIPause();

            GUIPlay._isPlayingGame = false;
            if (_turnOn.IsLoadCompleted) _turnOn.Play();

            LoadFromFile();
            
        }
        protected override void OnPaint(PaintEventArgs e)
        {
            if (GUIPause._isPause && frMain._statusGUI == EStatusGUI.MainMenuGUI)
            {
                frMain._statusGUI = EStatusGUI.PauseMenuGUI;
            }

            if (_makeNewGame == true)
            {
                _GUI[0] = new GUIPlay(this.CreateGraphics());
                ObjMap.LoadMap(1);
            }

            _GUI[(int)_statusGUI].Draw(this.CreateGraphics());
        }

       

        protected override void OnPaintBackground(PaintEventArgs e)
        {// Đừng xóa cái này dù nó ko làm gì 
            //base.OnPaintBackground(e);
        }

        public static string _filePath = "Highscore.ptl";
        private void LoadFromFile()
        {
            try
            {
                if (!File.Exists(_filePath))
                {
                    File.Create(_filePath);
                    File.SetAttributes(_filePath, FileAttributes.Hidden);
                }
                string _line;
                StreamReader _sr = new StreamReader(_filePath);

                _line = _sr.ReadLine();
                if (_line != null)
                {
                    _highScore = long.Parse(_line);
                }
                _sr.Close();
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }

        public static void SaveToFile()
        {
            try
            {
                if (File.Exists(_filePath)) File.SetAttributes(_filePath, FileAttributes.Hidden);
                else
                    File.Create(_filePath);

                FileStream f = new FileStream(_filePath, FileMode.OpenOrCreate);
                StreamWriter sw = new StreamWriter(f);
                //////////////////////////////////////////////////////////////////////////
                sw.WriteLine(_highScore);
                //////////////////////////////////////////////////////////////////////////
                sw.Flush();
                sw.Close();
            }
            catch (Exception exc)
            {
                MessageBox.Show("Writing...:\n" + exc.Message);
            }
        }

        #region Giúp chương trình nhận các phím bấm ...
       protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            if (keyData == Keys.Up
                || keyData == Keys.Down
                || keyData == Keys.Left
                || keyData == Keys.Right
                || keyData == Keys.Enter
                || keyData == Keys.A
                || keyData == Keys.S
                || keyData == Keys.D
                || keyData == Keys.W
                || keyData == Keys.Z
                || keyData == Keys.LShiftKey
                || keyData == Keys.RShiftKey
                || keyData == Keys.Escape
                || keyData == Keys.Back
                || keyData == Keys.Space
                )
            {
                OnKeyDown(new KeyEventArgs(keyData));
                return true;
            }
            return true; // base.ProcessCmdKey(ref msg, keyData);
        }
        #endregion

       private void frMain_KeyDown(object sender, KeyEventArgs e)
       {
           _GUI[(int)_statusGUI].InputKey(e.KeyCode);
           Invalidate();
       }

    }
}
